package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 *Created by xct on 2018/12/29
 *Description:
 */
class NewHomeViewModel:BaseViewModel() {
    private var homeBaseresh = MutableLiveData<FreshBean>()

    private var homeBaseResult: LiveData<ViewDataBean<RemoteResultBean<HomeBaseBean>>>? = null

    fun homeBase(): LiveData<ViewDataBean<RemoteResultBean<HomeBaseBean>>>? {
        if (homeBaseResult == null) {
            homeBaseResult = Transformations.switchMap(homeBaseresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getHomeBase(it.map)
                } else {
                    null
                }
            }
        }
        return homeBaseResult!!
    }

    fun freshHomeBase(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) homeBaseresh.value = FreshBean(map, isFresh)
    }
    private var homeWomenFavoritesFresh = MutableLiveData<FreshBean>()

    private var homeWomenFavoritesResult: LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? = null

    fun homeWomenFavorites(): LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? {
        if (homeWomenFavoritesResult == null) {
            homeWomenFavoritesResult = Transformations.switchMap(homeWomenFavoritesFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getFavoritesGoods(it.map)
                } else {
                    null
                }
            }
        }
        return homeWomenFavoritesResult!!
    }
    fun freshWomenFavorites(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) homeWomenFavoritesFresh.value = FreshBean(map, isFresh)
    }
    private var homeHighFavoritesFresh = MutableLiveData<FreshBean>()

    private var homeHighFavoritesResult: LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? = null

    fun homeHighFavorites(): LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? {
        if (homeHighFavoritesResult == null) {
            homeHighFavoritesResult = Transformations.switchMap(homeHighFavoritesFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getFavoritesGoods(it.map)
                } else {
                    null
                }
            }
        }
        return homeHighFavoritesResult!!
    }
    fun freshHighFavorites(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) homeHighFavoritesFresh.value = FreshBean(map, isFresh)
    }
    private var homeFavoritesFresh = MutableLiveData<FreshBean>()

    private var homeFavoritesResult: LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? = null

    fun homeFavorites(): LiveData<ViewDataBean<RemoteResultBean<HomeChiocenGoodsBean>>>? {
        if (homeFavoritesResult == null) {
            homeFavoritesResult = Transformations.switchMap(homeFavoritesFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getFavoritesGoods(it.map)
                } else {
                    null
                }
            }
        }
        return homeFavoritesResult!!
    }

    fun freshFavorites(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) homeFavoritesFresh.value = FreshBean(map, isFresh)
    }
    private var homeBroadcastFresh = MutableLiveData<FreshBean>()

    private var homeBroadcastResult: LiveData<ViewDataBean<RemoteResultBean<List<HomeBroadcastBean>>>>? = null

    fun homeBroadcast(): LiveData<ViewDataBean<RemoteResultBean<List<HomeBroadcastBean>>>>? {
        if (homeBroadcastResult == null) {
            homeBroadcastResult = Transformations.switchMap(homeBroadcastFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getBroadcast(it.map)
                } else {
                    null
                }
            }
        }
        return homeBroadcastResult!!
    }

    fun freshBroadcast(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) homeBroadcastFresh.value = FreshBean(map, isFresh)
    }
}